package com.doujia.pattern.singleton;

public class SingtetonInstance4Lazy {

    private static SingtetonInstance4Lazy instance;

    private SingtetonInstance4Lazy(){
    }

    public static SingtetonInstance4Lazy getInstance() {
        if(instance == null){
            instance = new SingtetonInstance4Lazy();
        }

        return  instance;
    }


    public void doSomething(){
        System.out.println(instance);
    }

    public static void main(String[] args) {

        Runnable task = () -> {
            String threadName = Thread.currentThread().getName();
            SingtetonInstance4Lazy.getInstance().doSomething();
            System.out.println("Hello " + threadName);
        };
        task.run();

        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);
        Thread thread3 = new Thread(task);
        Thread thread4 = new Thread(task);
        Thread thread5 = new Thread(task);
        Thread thread6 = new Thread(task);



        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();


/*        SingtetonInstance4Lazy instance1 = SingtetonInstance4Lazy.getInstance();
        SingtetonInstance4Lazy instance2 = SingtetonInstance4Lazy.getInstance();
        SingtetonInstance4Lazy instance3 = SingtetonInstance4Lazy.getInstance();
        SingtetonInstance4Lazy instance4 = SingtetonInstance4Lazy.getInstance();
        SingtetonInstance4Lazy instance5 = SingtetonInstance4Lazy.getInstance();
        SingtetonInstance4Lazy instance6 = SingtetonInstance4Lazy.getInstance();
        SingtetonInstance4Lazy instance7 = SingtetonInstance4Lazy.getInstance();

        System.out.println(instance1 == instance2);
        System.out.println(instance1 == instance3);
        System.out.println(instance1 == instance3);
        System.out.println(instance1 == instance4);
        System.out.println(instance1 == instance5);
        System.out.println(instance1 == instance6);
        System.out.println(instance1 == instance7);*/
    }
}
